﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Xml.Linq;

namespace Lightray.Hattrick.ChppLib.EntityTypes
{
    [ComVisible(true)]
    public class LeagueCountry : EntityBase
    {
        #region League id -> Country Map
        private  static Dictionary<int, Country> LEAGUE_ID_TO_COUNTRY_MAP = new Dictionary<int, Country>()
        {
            { 1, Country.Sweden },
            { 2, Country.England },
            { 3, Country.Germany },
            { 4, Country.Italy },
            { 5, Country.France },
            { 6, Country.Mexico },
            { 7, Country.Argentina },
            { 8, Country.USA },
            { 9, Country.Norway },
            { 11, Country.Denmark },
            { 12, Country.Finland },
            { 14, Country.Netherlands },
            { 15, Country.Oceania },
            { 16, Country.Brazil },
            { 17, Country.Canada },
            { 18, Country.Chile },
            { 19, Country.Colombia },
            { 20, Country.India },
            { 21, Country.Ireland },
            { 22, Country.Japan },
            { 23, Country.Peru },
            { 24, Country.Poland },
            { 25, Country.Portugal },
            { 26, Country.Scotland },
            { 27, Country.SouthAfrica },
            { 28, Country.Uruguay },
            { 29, Country.Venezuela },
            { 30, Country.SouthKorea },
            { 31, Country.Thailand },
            { 32, Country.Turkey },
            { 33, Country.Egypt },
            { 34, Country.China },
            { 35, Country.Russia },
            { 36, Country.Spain },
            { 37, Country.Romania },
            { 38, Country.Iceland },
            { 39, Country.Austria },
            { 44, Country.Belgium },
            { 45, Country.Malaysia },
            { 46, Country.Switzerland },
            { 47, Country.Singapore },
            { 50, Country.Greece },
            { 51, Country.Hungary },
            { 52, Country.CzechRepublic },
            { 53, Country.Latvia },
            { 54, Country.Indonesia },
            { 55, Country.Philippines },
            { 56, Country.Estonia },
            { 57, Country.Serbia },
            { 58, Country.Croatia },
            { 59, Country.HongKong },
            { 60, Country.ChineseTaipei },
            { 61, Country.Wales },
            { 62, Country.Bulgaria },
            { 63, Country.Israel },
            { 64, Country.Slovenia },
            { 66, Country.Lithuania },
            { 67, Country.Slovakia },
            { 68, Country.Ukraine },
            { 69, Country.BosniaHerzegovina },
            { 70, Country.Vietnam },
            { 71, Country.Pakistan },
            { 72, Country.Paraguay },
            { 73, Country.Ecuador },
            { 74, Country.Bolivia },
            { 75, Country.Nigeria },
            { 76, Country.FaroeIslands },
            { 77, Country.Morocco },
            { 79, Country.SaudiArabia },
            { 80, Country.Tunisia },
            { 81, Country.CostaRica },
            { 83, Country.UnitedArabEmirates },
            { 84, Country.Luxembourg },
            { 85, Country.Iran },
            { 88, Country.DominicanRepublic },
            { 89, Country.Cyprus },
            { 91, Country.Belarus },
            { 93, Country.NorthernIreland },
            { 94, Country.Jamaica },
            { 95, Country.Kenya },
            { 96, Country.Panama },
            { 97, Country.FYRMacedonia },
            { 98, Country.Albania },
            { 99, Country.Honduras },
            { 100, Country.ElSalvador },
            { 101, Country.Malta },
            { 102, Country.Kyrgyzstan },
            { 103, Country.Moldova },
            { 104, Country.Georgia },
            { 105, Country.Andorra },
            { 106, Country.Jordan },
            { 107, Country.Guatemala },
            { 110, Country.TrinidadTobago },
            { 111, Country.Nicaragua },
            { 112, Country.Kazakhstan },
            { 113, Country.Suriname },
            { 117, Country.Liechtenstein },
            { 118, Country.Algeria },
            { 119, Country.Mongolia },
            { 120, Country.Lebanon },
            { 121, Country.Senegal },
            { 122, Country.Armenia },
            { 123, Country.Bahrain },
            { 124, Country.Barbados },
            { 125, Country.CapeVerde },
            { 126, Country.IvoryCoast },
            { 127, Country.Kuwait },
            { 128, Country.Iraq },
            { 129, Country.Azerbaijan },
            { 130, Country.Angola },
            { 131, Country.Montenegro },
            { 132, Country.Bangladesh },
            { 133, Country.Yemen },
            { 134, Country.Oman },
            { 135, Country.Mozambique },
            { 136, Country.Brunei },
            { 137, Country.Ghana },
            { 138, Country.Cambodia },
            { 139, Country.Benin },
            { 140, Country.Syria },
            { 141, Country.Qatar },
            { 142, Country.Tanzania },
            { 143, Country.Uganda },
            { 144, Country.Maldives },
            { 145, Country.Uzbekistan },
            { 146, Country.Cameroon },
            { 147, Country.Cuba },
            { 148, Country.Palestine },
        };
        #endregion
       
        #region Properties
        public string Name { get; private set; }
        private string _currencyName;
        public string CurrencyName
        {
            get { return _currencyName; }
            private set
            {
                try
                {
                    _currencyName = Currency.GetSymbol(ToEnum());
                }
                catch (Exception)
                {
                    // Hattrick deafult
                    _currencyName = value;
                }                
            }
        }
        public int CurrencyRate { get; private set; }
        public int SeasonNumber { get; private set; }
        public int SeasonRound { get; private set; }
        public DateTime TrainingDate { get; private set; }
        public DateTime EconomyDate { get; private set; }
        public DateTime CupMatchDate { get; private set; }
        public DateTime SeriesMatchDate { get; private set; }
        public int LeagueID { get; private set; }
        public int CupID { get; private set; }
        #endregion

        protected LeagueCountry() { }

        public static LeagueCountry FromWorldXml(XDocument xml)
        {
            HattrickXmlReader reader = new HattrickXmlReader(xml);
            return new LeagueCountry()
            {
                ID = reader.ReadNumber("CountryID"),
                DataTime = reader.ReadDate("FetchedDate"),
                Name = reader.ReadString("CountryName"),
                CurrencyName = reader.ReadString("CurrencyName"),
                CurrencyRate = reader.ReadNumber("CurrencyRate"),
                SeasonNumber = reader.ReadNumber("Season"),
                SeasonRound = reader.ReadNumber("MatchRound"),
                TrainingDate = reader.ReadDate("TrainingDate"),
                EconomyDate = reader.ReadDate("EconomyDate"),
                CupMatchDate = reader.ReadDate("CupMatchDate"),
                SeriesMatchDate = reader.ReadDate("SeriesMatchDate"),
                LeagueID = reader.ReadNumber("LeagueID"),
                CupID = reader.ReadNumber("CupID"),
            };
        }

        public Country ToEnum()
        {
            if (!Enum.IsDefined(typeof(Country), ID))
                throw new Exception("Illegal country id");
            return (Country)ID;
        }

        public static Country LeagueIdToCountry(int leagueId)
        {
            if (LEAGUE_ID_TO_COUNTRY_MAP.ContainsKey(leagueId))
                return LEAGUE_ID_TO_COUNTRY_MAP[leagueId];

            throw new ArgumentException("Invalid league id", "leagueId");
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
  